1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package java.util.concurrent.atomic;
18  
19  /**
20   * GWT emulated version of {@link AtomicInteger}.  It's a thin wrapper
21   * around the primitive {@code int}.
22   *
23   * @author Hayward Chan
24   */
25  public class AtomicInteger extends Number implements java.io.Serializable {
26  
27    private int value;
28  
29    public AtomicInteger(int initialValue) {
30      value = initialValue;
31    }
32  
33    public AtomicInteger() {
34    }
35  
36    public final int get() {
37      return value;
38    }
39  
40    public final void set(int newValue) {
41      value = newValue;
42    }
43  
44    public final void lazySet(int newValue) {
45      set(newValue);
46    }
47  
48    public final int getAndSet(int newValue) {
49      int current = value;
50      value = newValue;
51      return current;
52    }
53  
54    public final boolean compareAndSet(int expect, int update) {
55      if (value == expect) {
56        value = update;
57        return true;
58      } else {
59        return false;
60      }
61    }
62  
63    public final int getAndIncrement() {
64      return value++;
65    }
66  
67    public final int getAndDecrement() {
68      return value--;
69    }
70  
71    public final int getAndAdd(int delta) {
72      int current = value;
73      value += delta;
74      return current;
75    }
76  
77    public final int incrementAndGet() {
78      return ++value;
79    }
80  
81    public final int decrementAndGet() {
82      return --value;
83    }
84  
85    public final int addAndGet(int delta) {
86      value += delta;
87      return value;
88    }
89  
90    @Override public String toString() {
91      return Integer.toString(value);
92    }
93  
94    public int intValue() {
95      return value;
96    }
97  
98    public long longValue() {
99      return (long) value;
100   }
101 
102   public float floatValue() {
103     return (float) value;
104   }
105 
106   public double doubleValue() {
107     return (double) value;
108   }
109 }